package com.jijs.service;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.jws.WebService;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.events.XMLEvent;
import javax.xml.ws.WebServiceContext;

import com.jijs.bean.User;
import com.jijs.dao.UserDAO;
import com.jijs.exception.UserException;
import com.sun.xml.ws.api.message.Header;
import com.sun.xml.ws.api.message.HeaderList;
import com.sun.xml.ws.developer.JAXWSProperties;

@WebService(endpointInterface="com.jijs.service.IUserService",
//		wsdlLocation="META-INF/wsdl/user.wsdl",
		wsdlLocation="WEB-INF/wsdl/user.wsdl",
		serviceName="userService",
		portName="userServicePort",
		targetNamespace="http://service.jijs.com/user/")
public class UserServiceImpl implements IUserService {

	private UserDAO userDAO = UserDAO.getInstance();
	
	@Resource
	private WebServiceContext ctx;
	
	@Override
	public void add(User user) throws UserException {
		checkRegister();
		userDAO.add(user);
	}

	private void checkRegister() {
		try {
			//消息头中获取相应的消息，并且进行相应的判断
			HeaderList headers = (HeaderList)ctx.getMessageContext().get(JAXWSProperties.INBOUND_HEADER_LIST_PROPERTY);
			QName name = new QName("http://service.jijs.com/user/", "licenceInfo");
			if(headers==null) throw new UserException("该功能需要进行权限控制");
			Header header = headers.get(name, true);
			if(header==null) throw new UserException("该功能需要进行权限控制");
			XMLStreamReader xsr = header.readHeader();
			User u = x2user(xsr);
			System.out.println(u.getNickname());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private User x2user(XMLStreamReader xsr) throws XMLStreamException {
		User u = new User();
		while(xsr.hasNext()){
			int event = xsr.next();
			if(event==XMLEvent.START_ELEMENT){
				System.out.println(xsr.getName().toString());
				String name = xsr.getName().toString();
				if(name.endsWith("username")){
					u.setUsername(xsr.getElementText());
				}else if(name.endsWith("password")){
					u.setPassword(xsr.getElementText());
				}else if(name.endsWith("nickname")){
					u.setNickname(xsr.getElementText());
				}
			}
		}
		return u;
	}

	@Override
	public void delete(String username) {
		userDAO.delete(username);
	}

	@Override
	public List<User> list() {
		return userDAO.list();
	}

	@Override
	public User login(String username, String password) throws UserException {
		return userDAO.login(username, password);
	}

	@Override
	public void upload(byte[] file) {
		FileOutputStream fos = null;  
        System.out.println(file);  
        try {  
            fos = new FileOutputStream("d:\\1.txt");  
            fos.write(file);  
            fos.flush();  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        } finally {  
            try {  
                fos.close();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  
	}	
	
	
	

}
